<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category_m extends FOX_Model
{

    const TBL = 'fox_category';

	function __construct ()
	{
		parent::__construct();
	}
	public function user_all_num()
	{
		$this->db->select('user_id');
		$this->db->where('user_id >',0);	
		$this->db->from('users');
		$total=$this->db->count_all_results();
		return $total;
	}

	public function add_cate($data)
	{
		$this->db->insert('category',$data);
	}
	
	public function update_cate($id,$data)
	{
		$this->db->where('id',$id)->update('category', $data);
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	public function del_cate($id)
	{
		$this->db->where('id',$id)->delete('category');
		// $this->db->where('pid',$node_id)->delete('category');
		
	}
	public function get_cates_by_pid($pid)
	{
		$this->db->select('*');
		$query = $this->db->where('pid',$pid)->get('category');
		return $query->result_array();
	}
	public function get_category_by_id($id)
	{
		$this->db->where('id',$id);
		$query = $this->db->get('category');
		return $query->row_array();
	}
	public function get_all_cates()
	{
		$this->db->select('id,pid,category_name');
        $this->db->from('category');
		$this->db->order_by('id', 'desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
	}
	public function get_cates_by_all_category($all_category,$pid=0){
		$this->db->select('*');
		$query = $this->db->where('all_category',$all_category)->where('pid',$pid)->get('category');
		return $query->result_array();
	}
    public function get_sub_cates_by_all_category($pid=0){
        $this->db->select('*');
        $query = $this->db->where('pid',$pid)->get('category');
        return $query->result_array();
    }

    public function get_sub_category(){
        $this->db->select('*');
        $query = $this->db->where('pid !=',0)->get('category');
        return $query->result_array();
    }

    public function get_row_by_catename($catename){
        $this->db->select('*');
        $query = $this->db->where('category_name',$catename)->get('category');
        return $query->row_array();
    }

    public function get_cates_by($where)
    {
        $this->db->select('*');
        $query = $this->db->where($where)->get('category');
        return $query->result_array();
    }
}